package com.example.hello;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class CategoryTable extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;
	// Database Name
	private static final String DATABASE_NAME = "TechDatabase";
	// Category table name
	private static final String TABLE_TEST = "Category";
	// Category Table Columns names
	private static final String CAT_ID = "id";
	private static final String CAT_NAME = "name";

	public CategoryTable(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table " + TABLE_TEST
			+ "(" + CAT_ID + " integer primary key autoincrement, " + CAT_NAME
			+ " text not null);";

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Log.w(MySQLiteHelper.class.getName(),
		// "Upgrading database from version " + oldVersion + " to "
		// + newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEST);
		onCreate(db);
	}

	public void addCategory(int cat_id, String cat_name) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(CAT_ID, cat_id); // Contact Name
		values.put(CAT_NAME, cat_name); // Contact Phone
		// Inserting Row
		db.insert(TABLE_TEST, null, values);
		db.close(); // Closing database connection
	}
	
    public void getAllCategory() {        
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_TEST;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {                
                Log.d(cursor.getString(0), cursor.getString(1)); 
            } while (cursor.moveToNext());
        }
         
    }
	
}